package com.bluefox.hospital.service.impl;

import com.bluefox.common.web.exception.CustomRuntimeException;
import com.bluefox.hospital.model.Doctor;
import com.bluefox.hospital.repository.DepartmentRepository;
import com.bluefox.hospital.repository.DoctorRepository;
import com.bluefox.hospital.service.DoctorService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * DoctorService 实现类
 */
@Service
@Transactional
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    @Transactional(readOnly = true)
    public Doctor get(@NonNull Long id) {
        final Doctor model = doctorRepository.findById(id).get();
        if (model == null) {
            throw new CustomRuntimeException("404", String.format("查找的资源[%s]不存在.", id));
        }
        return model;
    }

    @Override
    public Doctor create(Doctor model) {
        // TODO: 业务逻辑
        return doctorRepository.save(model);
    }

    @Override
    public Doctor update(Doctor model) {
        // TODO: 业务逻辑
        return doctorRepository.save(model);
    }

    @Override
    public void delete(@NonNull Long id) {
        // TODO: 业务逻辑
        doctorRepository.deleteById(id);
    }

    @Override
    public Page<Doctor> findByDepartmentId(Long departmentId, Pageable pageable) {
        if (departmentId == 1) {
            return doctorRepository.findAll(pageable);
        } else {
            return doctorRepository.findByDepartmentId(departmentId, pageable);
        }

    }
}
